package com.dzu.gulimall.order.web;

import com.dzu.gulimall.order.service.OrderService;
import com.dzu.gulimall.order.vo.OrderConfirmVo;
import com.dzu.gulimall.order.vo.OrderSubmitResponseVo;
import com.dzu.gulimall.order.vo.OrderSubmitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.concurrent.ExecutionException;

/**
 * @author ZhaoDong
 * @date 2022/7/15 22:21
 * @description gulimall
 */
@Controller
public class OrderWebController {

    @Autowired
    private OrderService orderService;

    /**
     * 去订单结算页 需要的数据
     * @param model
     * @return
     */
    @GetMapping("/toTrade")
    public Object toTrade(Model model) throws ExecutionException, InterruptedException {
        OrderConfirmVo vo = orderService.confirmOrder();

        model.addAttribute("confirmOrder", vo);
        // 展示订单确认的属性
        return "confirm";
//        return vo;
    }

    /**
     * 下单功能
     *
     * @param vo
     * @return
     */
    @PostMapping("/submitOrder")
    public Object submitOrder(OrderSubmitVo vo, Model model) {

        OrderSubmitResponseVo responseVo = orderService.submitOrder(vo);
        // 下单失败回到订单确认页重新确认订单信息
        // 下单成功来到支付选择页
        if (responseVo.getCode() == 0) {
            model.addAttribute("submitOrderResp", responseVo);
            return "pay";
        }
        String str = "下单失败";
        Integer code = responseVo.getCode();
        switch (code) {
            case 1:
                str += "订单信息过期，请刷新再次提交";
                break;
            case 2:
                str += "订单商品价格发生变化，请确认后再次提交";
                break;
            case 3:
                str += "商品库存不足";
                break;
            default:
                return "";
        }
        model.addAttribute("msg", str);

        return "redirect:http://order.gulimall.com/toTrade";
    }

}
